<template>
  <!-- ASN单表单弹窗 -->
  <div>
    <el-dialog
      v-model="visibleDialog"
      :title="props.title"
      width="1100"
      draggable
      @close="closeDialog"
    >
      <el-form
        ref="formRef"
        :model="formData"
        :inline="false"
        :rules="rules"
        label-width="130"
      >
        <h4 class="mb-[20px] mt-[0px]">基本数据</h4>
        <el-card>
          <div>
            <el-row :gutter="16">
<!--              <el-col :span="8">-->
<!--                <el-form-item label="送货单号" prop="asnCode">-->
<!--                  <el-input-->
<!--                    v-if="!props.isReadonly"-->
<!--                    v-model="formData.asnCode"-->
<!--                    placeholder="请输入送货单号"-->
<!--                    clearable-->
<!--                  />-->
<!--                  <span v-else>{{ formData.asnCode }}</span>-->
<!--                </el-form-item>-->
<!--              </el-col>-->
              <el-col :span="8">
                <el-form-item label="供应商" prop="partnerCode">
                  <el-input
                    v-if="!props.isReadonly"
                    v-model="formData.partnerCode"
                    placeholder="请选择供应商"
                    clearable
                  >
                    <template #append>
                      <el-button
                        type="success"
                        plain
                        @click="showWorkOrderSelect2"
                        >选择</el-button
                      >
                    </template>
                  </el-input>
                  <span v-else>{{ formData.partnerName }}</span>
                </el-form-item>
              </el-col>
<!--              <el-col :span="8">-->
<!--                <el-form-item label="送货日期" prop="asnDate">-->
<!--                  <el-date-picker-->
<!--                    v-if="!props.isReadonly"-->
<!--                    v-model="formData.asnDate"-->
<!--                    value-format="YYYY-MM-DD HH:mm:ss"-->
<!--                    format="YYYY-MM-DD HH:mm:ss"-->
<!--                    clearable-->
<!--                    type="datetime"-->
<!--                    placeholder="请选择送货日期"-->
<!--                    style="width: 100%"-->
<!--                  />-->
<!--                  <span v-else>{{ formData.asnDate }}</span>-->
<!--                </el-form-item>-->
<!--              </el-col>-->
<!--              <el-col :span="8">-->
<!--                <el-form-item label="预计到货日期" prop="arriveDate">-->
<!--                  <el-date-picker-->
<!--                    v-if="!props.isReadonly"-->
<!--                    v-model="formData.arriveDate"-->
<!--                    value-format="YYYY-MM-DD HH:mm:ss"-->
<!--                    format="YYYY-MM-DD HH:mm:ss"-->
<!--                    clearable-->
<!--                    type="datetime"-->
<!--                    placeholder="请选择预计到货日期"-->
<!--                    style="width: 100%"-->
<!--                  />-->
<!--                  <span v-else>{{ formData.arriveDate }}</span>-->
<!--                </el-form-item>-->
<!--              </el-col>-->
<!--              <el-col :span="8">-->
<!--                <el-form-item label="运输方式" prop="modeTransport">-->
<!--                  <el-select-->
<!--                    v-if="!props.isReadonly"-->
<!--                    v-model="formData.modeTransport"-->
<!--                    placeholder="请选择运输方式"-->
<!--                    clearable-->
<!--                  >-->
<!--                    <el-option-->
<!--                      v-for="item in dictionaryStore.filterDictList('modeTransportDic')"-->
<!--                      :key="item.value"-->
<!--                      :label="item.label"-->
<!--                      :value="item.value"-->
<!--                    />-->
<!--                  </el-select>-->
<!--                  <span v-else>{{-->
<!--                      useDictionary("ShipmentStatus", formData.modeTransport)-->
<!--                    }}</span>-->
<!--                </el-form-item>-->
<!--              </el-col>-->
<!--              <el-col :span="8">-->
<!--                <el-form-item label="运单号" prop="trackingNumber">-->
<!--                  <el-input-->
<!--                    v-if="!props.isReadonly"-->
<!--                    v-model="formData.trackingNumber"-->
<!--                    placeholder="请输入运单号"-->
<!--                    clearable-->
<!--                  />-->
<!--                  <span v-else>{{ formData.trackingNumber }}</span>-->
<!--                </el-form-item>-->
<!--              </el-col>-->
<!--              <el-col :span="8">-->
<!--                <el-form-item label="承运人" prop="carrier">-->
<!--                  <el-input-->
<!--                    v-if="!props.isReadonly"-->
<!--                    v-model="formData.carrier"-->
<!--                    placeholder="请输入承运人"-->
<!--                    clearable-->
<!--                  />-->
<!--                  <span v-else>{{ formData.carrier }}</span>-->
<!--                </el-form-item>-->
<!--              </el-col>-->
<!--              <el-col :span="8">-->
<!--                <el-form-item label="发货联系人" prop="shippingContactPerson">-->
<!--                  <el-input-->
<!--                    v-if="!props.isReadonly"-->
<!--                    v-model="formData.shippingContactPerson"-->
<!--                    placeholder="请输入发货联系人"-->
<!--                    clearable-->
<!--                  />-->
<!--                  <span v-else>{{ formData.shippingContactPerson }}</span>-->
<!--                </el-form-item>-->
<!--              </el-col>-->
<!--              <el-col :span="8">-->
<!--                <el-form-item-->
<!--                  label="发货联系人电话"-->
<!--                  prop="shippingContactPhone"-->
<!--                >-->
<!--                  <el-input-->
<!--                    v-if="!props.isReadonly"-->
<!--                    v-model="formData.shippingContactPhone"-->
<!--                    placeholder="请输入发货联系人电话"-->
<!--                    clearable-->
<!--                  />-->
<!--                  <span v-else>{{ formData.shippingContactPhone }}</span>-->
<!--                </el-form-item>-->
<!--              </el-col>-->
<!--              <el-col :span="8">-->
<!--                <el-form-item label="车牌号" prop="plateNumber">-->
<!--                  <el-input-->
<!--                    v-if="!props.isReadonly"-->
<!--                    v-model="formData.plateNumber"-->
<!--                    placeholder="请输入车牌号"-->
<!--                    clearable-->
<!--                  />-->
<!--                  <span v-else>{{ formData.plateNumber }}</span>-->
<!--                </el-form-item>-->
<!--              </el-col>-->
<!--              <el-col :span="8">-->
<!--                <el-form-item label="发货状态" prop="shipmentStatus">-->
<!--                  <el-select-->
<!--                    v-if="!props.isReadonly"-->
<!--                    v-model="formData.shipmentStatus"-->
<!--                    placeholder="请选择发货状态"-->
<!--                    clearable-->
<!--                  >-->
<!--                    <el-option-->
<!--                      v-for="item in dictionaryStore.filterDictList('ShipmentStatus')"-->
<!--                      :key="item.value"-->
<!--                      :label="item.label"-->
<!--                      :value="item.value"-->
<!--                    />-->
<!--                  </el-select>-->
<!--                  <span v-else>{{-->
<!--                      useDictionary("ShipmentStatus", formData.shipmentStatus)-->
<!--                  }}</span>-->
<!--                </el-form-item>-->
<!--              </el-col>-->
<!--              <el-col :span="8">-->
<!--                <el-form-item label="收货状态" prop="receiveStatus">-->
<!--                  <el-select-->
<!--                    v-if="!props.isReadonly"-->
<!--                    v-model="formData.receiveStatus"-->
<!--                    placeholder="请选择发货状态"-->
<!--                    clearable-->
<!--                  >-->
<!--                    <el-option-->
<!--                      v-for="item in dictionaryStore.filterDictList('ReceiveStatus')"-->
<!--                      :key="item.value"-->
<!--                      :label="item.label"-->
<!--                      :value="item.value"-->
<!--                    />-->
<!--                  </el-select>-->
<!--                  <span v-else>{{-->
<!--                    useDictionary("ReceiveStatus", formData.receiveStatus)-->
<!--                  }}</span>-->
<!--                </el-form-item>-->
<!--              </el-col>-->
<!--              <el-col :span="8">-->
<!--                <el-form-item label="收货时间" prop="takeTime">-->
<!--                  <el-date-picker-->
<!--                    v-if="!props.isReadonly"-->
<!--                    v-model="formData.takeTime"-->
<!--                    value-format="YYYY-MM-DD HH:mm:ss"-->
<!--                    format="YYYY-MM-DD HH:mm:ss"-->
<!--                    clearable-->
<!--                    type="datetime"-->
<!--                    placeholder="请选择收货时间"-->
<!--                    style="width: 100%"-->
<!--                  />-->
<!--                  <span v-else>{{ formData.takeTime }}</span>-->
<!--                </el-form-item>-->
<!--              </el-col>-->
<!--              <el-col :span="8">-->
<!--                <el-form-item label="收货人" prop="takeUserId">-->
<!--                  <el-input-->
<!--                    v-if="!props.isReadonly"-->
<!--                    v-model="formData.takeUserId"-->
<!--                    placeholder="请输入收货人"-->
<!--                    clearable-->
<!--                  />-->
<!--                  <span v-else>{{ formData.takeUserName }}</span>-->
<!--                </el-form-item>-->
<!--              </el-col>-->
              <el-col :span="24">
                <el-form-item class="full-line" label="备注" prop="remarks">
                  <el-input
                    type="textarea"
                    v-if="!props.isReadonly"
                    v-model="formData.remarks"
                    placeholder="请输入备注"
                    clearable
                    style="width: 100%"
                  />
                  <span v-else>{{ formData.remarks }}</span>
                </el-form-item>
              </el-col>
            </el-row>
          </div>
        </el-card>
        <h4 class="mb-[20px] mt-[20px]">物料详情</h4>
        <el-divider />

        <el-card>
          <div class="formTable">
            <el-row :gutter="16">
              <el-col :span="8">
                <el-form-item
                  label="物料"
                  prop="details.materialCode"
                  :rules="rules.sonmaterialCode"
                >
                  <el-input
                    v-if="!props.isReadonly"
                    v-model="formData.details.materialCode"
                    placeholder="请选择物料"
                    clearable
                  >
                    <template #append>
                      <el-button
                        type="success"
                        plain
                        @click="showWorkOrderSelect"
                        >选择</el-button
                      >
                    </template>
                  </el-input>
                  <span v-else>{{ formData.details.materialName }}</span>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item
                  label="数量"
                  prop="details.receivedQuantity"
                  :rules="rules.sonreceivedQuantity"
                >
                  <el-input-number
                    v-if="!props.isReadonly"
                    v-model="formData.details.receivedQuantity"
                    placeholder="请输入数量"
                    clearable
                    :min="1"
                    :step="1"
                    :precision="3"
                    style="width: 100%"
                  />
                  <span v-else>{{ formData.details.receivedQuantity }}</span>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item
                  label="数量单位"
                  prop="details.unitCode"
                  :rules="rules.sonunitCode"
                >
                  <span>{{
                      useDictionary("MeasurementUnit", formData.details.unitCode || "")
                    }}</span>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item
                  label="生产批次"
                  prop="details.supplyBatch"
                  :rules="rules.sonsupplyBatch"
                >
                  <el-input
                    v-if="!props.isReadonly"
                    v-model="formData.details.supplyBatch"
                    placeholder="请输入生产批次"
                    clearable
                  />
                  <span v-else>{{ formData.details.supplyBatch }}</span>
                </el-form-item>
              </el-col>
<!--              <el-col :span="8">-->
<!--                <el-form-item-->
<!--                  label="供货数量"-->
<!--                  prop="details.quantity"-->
<!--                  :rules="rules.sonquantity"-->
<!--                >-->
<!--                  <el-input-number-->
<!--                    v-if="!props.isReadonly"-->
<!--                    v-model="formData.details.quantity"-->
<!--                    placeholder="请输入供货数量"-->
<!--                    clearable-->
<!--                    :min="1"-->
<!--                    :step="1"-->
<!--                    :precision="3"-->
<!--                    style="width: 100%"-->
<!--                  />-->
<!--                  <span v-else>{{ formData.details.quantity }}</span>-->
<!--                </el-form-item>-->
<!--              </el-col>-->
              <el-col :span="8">
                <el-form-item
                  label="批次"
                  prop="details.batch"
                  :rules="rules.sonbatch"
                >
                  <el-input
                    v-if="!props.isReadonly"
                    v-model="formData.details.batch"
                    placeholder="请输入批次"
                    clearable
                  />
                  <span v-else>{{ formData.details.batch }}</span>
                </el-form-item>
              </el-col>
<!--              <el-col :span="8">-->
<!--                <el-form-item label="仓库" prop="details.warehouseCode">-->
<!--                  <el-input-->
<!--                    v-if="!props.isReadonly"-->
<!--                    v-model="formData.details.warehouseCode"-->
<!--                    placeholder="请选择仓库"-->
<!--                    clearable-->
<!--                  >-->
<!--                    <template #append>-->
<!--                      <el-button-->
<!--                        type="success"-->
<!--                        plain-->
<!--                        @click="showWorkOrderSelect3"-->
<!--                        >选择</el-button-->
<!--                      >-->
<!--                    </template>-->
<!--                  </el-input>-->
<!--                  <span v-else>{{ formData.details.warehouseName }}</span>-->
<!--                </el-form-item>-->
<!--              </el-col>-->
<!--              <el-col :span="8">-->
<!--                <el-form-item label="仓位" prop="details.binCode">-->
<!--                  <el-input-->
<!--                    v-if="!props.isReadonly"-->
<!--                    v-model="formData.details.binCode"-->
<!--                    placeholder="请选择仓位"-->
<!--                    clearable-->
<!--                  >-->
<!--                    <template #append>-->
<!--                      <el-button-->
<!--                        type="success"-->
<!--                        plain-->
<!--                        @click="showWorkOrderSelect4"-->
<!--                        >选择</el-button-->
<!--                      >-->
<!--                    </template>-->
<!--                  </el-input>-->
<!--                  <span v-else>{{ formData.details.binName }}</span>-->
<!--                </el-form-item>-->
<!--              </el-col>-->
              <el-col :span="24">
                <el-form-item
                  class="full-line"
                  label="备注"
                  prop="details.remarks"
                  :rules="rules.sonremarks"
                >
                  <el-input
                    type="textarea"
                    v-if="!props.isReadonly"
                    v-model="formData.details.remarks"
                    placeholder="请输入备注"
                    clearable
                    style="width: 100%"
                  />
                  <span v-else>{{ formData.details.remarks }}</span>
                </el-form-item>
              </el-col>
            </el-row>
          </div>
        </el-card>
      </el-form>

      <template #footer>
        <div class="dialog-footer">
          <el-button
            v-if="!props.isReadonly"
            type="primary"
            :loading="loading"
            @click="handleSubmit"
            >确 定</el-button
          >
          <el-button @click="closeDialog">取 消</el-button>
        </div>
      </template>
    </el-dialog>

    <material-select
      :visible="selectDialogSELECT.visible"
      @change-visible="changeVisibleSELECT"
      :brekList="brekList"
    />
    <partner-select
      :visible="selectDialogSELECT2.visible"
      @change-visible="changeVisibleSELECT2"
      :brekList="brekList2"
    />
  </div>
</template>

<script setup lang="ts">
import request from "@/utils/request";
import { useDictStoreHook } from "@/store/modules/dictionary"; // 引入
import partnerSelectDialog from "@/views/wms/base/mdmpartner/index.vue";
import { useDictionary } from "@/utils/useTable";
const dictionaryStore = useDictStoreHook();

defineOptions({
  name: "WMSSRMASNADDORUPDATE",
  inheritAttrs: false,
});

const emit = defineEmits(["reset-table", "change-visible"]);

const props = defineProps({
  visible: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
  title: {
    type: String,
    default: () => {
      return "";
    },
  },
  data: {
    type: Object,
    default: () => {
      return {};
    },
  },
  isReadonly: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
});


//取当前时间赋值批次默认值
function getCurrentDate6Digits() {
  const date = new Date();
  const year = date.getFullYear().toString().slice(-2);
  const month = (date.getMonth() + 1).toString().padStart(2, '0');
  const day = date.getDate().toString().padStart(2, '0');
  return year + month + day;
}

/** 弹窗显示 **/
const visibleDialog = ref(false);
watch(
  () => props.visible,
  (newVal: boolean) => {
    visibleDialog.value = newVal;
    setTimeout(() => {
      formRef.value.clearValidate();
    }, 50);
  }
);

/** formData定义表单数据 */
const formRef = ref(ElForm);
let formData = reactive({
  orderType: "4",
  addType: "1",
  orderNo: "",
  asnCode: "",
  partnerId: "",
  partnerCode: "",
  partnerName: "",
  asnDate: "",
  arriveDate: "",
  modeTransport: "",
  trackingNumber: "",
  storageLocationId: "",
  storageLocationCode: "",
  storageLocationName: "",
  carrier: "",
  shippingContactPerson: "",
  shippingContactPhone: "",
  plateNumber: "",
  shipmentStatus: "",
  receiveStatus: "",
  remarks: "",
  sapId: "",
  barCode: "",
  details: {
    materialId: "",
    materialCode: "",
    materialName: "",
    supplyBatch: getCurrentDate6Digits(),
    quantity: "",
    receivedQuantity: "",
    unitCode: "",
    remarks: "",
    batch: getCurrentDate6Digits()
  },
});

/** rules规则 */
const rules = reactive({
  asnCode: [
    { required: true, message: "请输入送货单号", trigger: "blur" },
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  partnerCode: [
    { required: true, message: "请选择供应商", trigger: "blur" },
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  asnDate: [
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  arriveDate: [],
  modeTransport: [],
  trackingNumber: [
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  carrier: [
    { min: 1, max: 32, message: "长度在 1 到 32 个字符", trigger: "blur" },
  ],
  shippingContactPerson: [
    { min: 1, max: 32, message: "长度在 1 到 32 个字符", trigger: "blur" },
  ],
  shippingContactPhone: [
    { min: 1, max: 32, message: "长度在 1 到 32 个字符", trigger: "blur" },
  ],
  plateNumber: [
    { min: 1, max: 32, message: "长度在 1 到 32 个字符", trigger: "blur" },
  ],
  shipmentStatus: [
    { min: 1, max: 32, message: "长度在 1 到 32 个字符", trigger: "blur" },
  ],
  receiveStatus: [],
  remarks: [
    { min: 1, max: 255, message: "长度在 1 到 255 个字符", trigger: "blur" },
  ],
  createTime: [],
  createdUserId: [
    { min: 1, max: 32, message: "长度在 1 到 32 个字符", trigger: "blur" },
  ],
  createdUserName: [
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  lastUpdateTime: [],
  lastUpdatedUserId: [
    { min: 1, max: 32, message: "长度在 1 到 32 个字符", trigger: "blur" },
  ],
  lastUpdatedUserName: [
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  deleted: [],
  takeTime: [],
  takeUserId: [
    { min: 1, max: 32, message: "长度在 1 到 32 个字符", trigger: "blur" },
  ],
  takeUserName: [
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  addType: [],
  barCode: [
    { min: 1, max: 255, message: "长度在 1 到 255 个字符", trigger: "blur" },
  ],
  sonasnId: [
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  sonorderId: [
    { min: 1, max: 32, message: "长度在 1 到 32 个字符", trigger: "blur" },
  ],
  sonmaterialId: [
    { min: 1, max: 32, message: "长度在 1 到 32 个字符", trigger: "blur" },
  ],
  sonmaterialCode: [
    { required: true, message: "请选择物料", trigger: "blur" },
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  sonmaterialName: [
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  sonsupplyBatch: [
    { required: true, message: "请输入生产批次", trigger: "blur" },
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  sonquantity: [
    { required: true, message: "请输入数量", trigger: "blur" },
    {
      type: "number",
      min: 1,
      message: "数量不能小于1",
      trigger: "blur",
    },
  ],
  sonreceivedQuantity: [
    { required: true, message: "请输入收货数量", trigger: "blur" },
    {
      type: "number",
      min: 1,
      message: "收货数量不能小于1",
      trigger: "blur",
    },
  ],
  sonremarks: [
    { min: 1, max: 255, message: "长度在 1 到 255 个字符", trigger: "blur" },
  ],
  soncreateTime: [],
  soncreatedUserId: [
    { min: 1, max: 32, message: "长度在 1 到 32 个字符", trigger: "blur" },
  ],
  soncreatedUserName: [
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  sonlastUpdateTime: [],
  sonlastUpdatedUserId: [
    { min: 1, max: 32, message: "长度在 1 到 32 个字符", trigger: "blur" },
  ],
  sonlastUpdatedUserName: [
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  sondeleted: [],
  sonisCheck: [],
  sonbatch: [
    { min: 1, max: 255, message: "长度在 1 到 255 个字符", trigger: "blur" },
  ],
  sonqualityResult: [],
  sonwarehouseId: [
    { min: 1, max: 32, message: "长度在 1 到 32 个字符", trigger: "blur" },
  ],
  sonwarehouseCode: [
    { required: true, message: "请选择仓库", trigger: "blur" },
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  sonwarehouseName: [
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  sonbinId: [
    { min: 1, max: 32, message: "长度在 1 到 32 个字符", trigger: "blur" },
  ],
  sonbinCode: [
    { required: true, message: "请选择仓位", trigger: "blur" },
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  sonbinName: [
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  sonbarCode: [
    { min: 1, max: 255, message: "长度在 1 到 255 个字符", trigger: "blur" },
  ],
  sonqualityState: [],
});

const isValidated = ref(false);
/** 删除行 */
const handleDelete = (index: any) => {
  formData.details.splice(index, 1);
  isValidated.value && formRef.value.validate(() => {});
};

const details = {
  materialId: "",
  materialCode: "",
  materialName: "",
  supplyBatch: "",
  quantity: "",
  receivedQuantity: "",
  unitCode: "",
  remarks: "",
  isCheck: "",
  poSa: "",
  poSaItem: "",
  batch: "",
  qualityResult: "",
  warehouseId: "",
  warehouseCode: "",
  warehouseName: "",
  storageSectionId: "",
  storageSectionCode: "",
  storageSectionName: "",
  shelfId: "",
  shelfCode: "",
  shelfName: "",
  binId: "",
  binCode: "",
  binName: "",
  barCode: "",
  qualityState: "",
  sapId: "",
  isDeposit: "",
};

/** 添加行 */
const handleAdd = () => {
  let sondata = JSON.parse(JSON.stringify(details));
  formData.details = sondata;
};

/** 编辑时赋值 */
watch(
  () => props.data,
  (newVal: any) => {
    formData.id = "";
    if (newVal?.id) {
      Object.assign(formData, props.data);
      // 如果details是数组，取第一个元素
      if (Array.isArray(formData.details) && formData.details.length > 0) {
        formData.details = formData.details[0];
      }
    }
  }
);

/** 关闭表单弹窗 */
function closeDialog() {
  visibleDialog.value = false;
  emit("change-visible", false);
  resetForm();
}

/** 重置表单 */
function resetForm() {
  formRef.value.resetFields();
  formRef.value.clearValidate();
  formData.details = JSON.parse(JSON.stringify(details));
  formData.orderNo = "";
  formData.asnCode = "";
  formData.partnerId = "";
  formData.partnerCode = "";
  formData.partnerName = "";
  formData.asnDate = "";
  formData.arriveDate = "";
  formData.modeTransport = "";
  formData.trackingNumber = "";
  formData.carrier = "";
  formData.shippingContactPerson = "";
  formData.shippingContactPhone = "";
  formData.plateNumber = "";
  formData.shipmentStatus = "";
  formData.receiveStatus = "";
  formData.addType = "";
  formData.sapId = "";
  formData.barCode = "";
  formData.details = {
    ...JSON.parse(JSON.stringify(details)),
    supplyBatch: getCurrentDate6Digits(),
    batch: getCurrentDate6Digits(),
    receivedQuantity: 1
  };

}

const loading = ref(false);

/** ASN单保存提交 */
function handleSubmit() {
  formRef.value.validate(async (valid: any) => {
    if (valid) {
      loading.value = true;
      const text = formData.id ? "编辑" : "新增";

      // 构造提交数据，details 按后端期望传对象（非数组）
      const submitData = {
        ...formData,
        details: [formData.details]
      };

      await request({
        url: "/wms/srmasn/save",
        method: "post",
        data: submitData,
      }).finally(() => {
        loading.value = false;
      });
      ElMessage.success(text + `成功`);
      closeDialog();
      emit("reset-table", formData.id);
      isValidated.value = false;
    } else {
      isValidated.value = true;
    }
  });
}

/** 物料弹窗操作 **/
const brekList = ref([]);
const selectDialogSELECT = ref({
  visible: false,
});
const changeVisibleSELECT = (val: boolean, data: any) => {
  selectDialogSELECT.value.visible = val;
  if (data) {
    formData.details.materialId = data.id;
    formData.details.materialName = data.materialName;
    formData.details.materialCode = data.materialCode;
    formData.details.unitCode = data.unitCode;
  }
};
const showWorkOrderSelect = () => {
  selectDialogSELECT.value.visible = true;
};

/** 供应商弹窗操作 **/
const selectDialogSELECT2 = ref({
  visible: false,
});
const changeVisibleSELECT2 = (val: boolean, data: any) => {
  selectDialogSELECT2.value.visible = val;
  if (data) {
    formData.partnerId = data.partnerId;
    formData.partnerName = data.partnerName;
    formData.partnerCode = data.partnerCode;
  }
};

const brekList2 = ref([]);
const showWorkOrderSelect2 = () => {
  selectDialogSELECT2.value.visible = true;
};

//发货状态
const shipmentStatusOptions = [
  { label: "待发货", value: "0" },
  { label: "已发货", value: "1" },
];
const shipmentStatusMap = Object.fromEntries(
  shipmentStatusOptions.map((item) => [item.value, item.label])
);

//收货状态
const receiveStatusOptions = [
  { label: "待收货", value: "0" },
  { label: "已收货", value: "1" },
];
const receiveStatusMap = Object.fromEntries(
  receiveStatusOptions.map((item) => [item.value, item.label])
);
</script>

<style lang="scss" scoped>
h4 {
  font-weight: 400;
  color: #000;
  padding-left: 10px;
  font-size: 14px;
  line-height: 14px;
}
.formTable {
  :deep(.el-form-item) {
    margin-right: 0 !important;
  }
}
// 统一表单控件宽度，让列内控件对齐
:deep(.el-form-item__content .el-input),
:deep(.el-form-item__content .el-select),
:deep(.el-form-item__content .el-date-editor),
:deep(.el-form-item__content .el-input-number),
:deep(.el-form-item__content .el-input__wrapper),
:deep(.el-form-item__content .el-select .el-input__wrapper),
:deep(.el-form-item__content .el-date-editor .el-input__wrapper) {
  width: 100% !important;
}
// 让整行表单项的内容占满宽度
:deep(.full-line .el-form-item__content) {
  width: 100% !important;
}
:deep(.full-line .el-input),
:deep(.full-line .el-textarea),
:deep(.full-line .el-input__wrapper),
:deep(.full-line .el-textarea__inner) {
  width: 100% !important;
}
:deep(.el-dialog .el-dialog__body) {
  overflow-y: auto;
  padding-left: 20px !important;
  padding-right: 20px !important;
}
// 卡片内容左右留白
:deep(.el-card__body) {
  padding-left: 20px !important;
  padding-right: 50px !important;
}
:deep(.search-container .el-input.el-input--default.el-input--suffix) {
  // 固定宽度
  width: 190px !important;
}
:deep(.search-container .el-form-item--default .el-form-item__label) {
  // 固定宽度
  justify-content: end;
}
</style>
